package ru.vsu.amm.fuzzy.problems;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import ru.vsu.amm.fuzzy.LOWA;
import ru.vsu.amm.fuzzy.Quantifier;
import ru.vsu.amm.fuzzy.Scale;

/**
 * Задача о групповом отношении предпочтения экспертов.
 * @author Иванов Илья
 * @since 05 января 2013
 *
 */
public class GroupPreferences {
	
	private static final double A = 0.25;
	private static final double B = 0.75;
	
	public static void main(String[] args) {
		if (args.length < 2)
			return;
		
		boolean half;
		// H - Half, M - Majority
		if (!(half = args[1].equalsIgnoreCase("H")) && !args[1].equalsIgnoreCase("M"))
			return;
		
		try (BufferedReader rdr = new BufferedReader(new FileReader(args[0]));
			BufferedWriter wrt = new BufferedWriter(new FileWriter(args[0] + "-out.txt"))) {
			
			String line;
			while ((line = rdr.readLine()) != null) {
				String[] scoresStrings = line.split(" ");
				List<Scale> scores = new ArrayList<>();
				for (String score : scoresStrings) {
					if (score.trim().isEmpty())
						continue;
					
					try {
						scores.add(Scale.valueOf(score.toUpperCase()));
					} catch (Exception e) {
						System.out.println("Неверное значение оценки");
						return;
					}
				}
				if (scores.isEmpty())
					continue;
				
				Quantifier quantifier = half ? new Quantifier.Half(scores.size()) : new Quantifier.Majority(A, B);
				double[] weights = quantifier.getWeightVector(scores.size());
				
				wrt.write("Вектор весовых коэффициентов:");
				wrt.newLine();
				for (double weight : weights) {
					wrt.write(String.valueOf(weight));
					wrt.newLine();
				}
				
				wrt.write("orness = " + String.valueOf(Quantifier.orness(weights)));
				wrt.newLine();
				
				wrt.write("LOWA = " + LOWA.calc(scores, weights).toString());
				wrt.newLine();
				wrt.newLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}